﻿"use strict"

exports.dau = function(){
    let branch = this.branch;
    let data = {"branch":branch,"server":{},"opensdk":{}};
    data['sid'] = this.get("sid",'int')||1;
    data['sdk'] = this.get("sdk",'string');
    data['format'] = this.config("dau");
    this.view = "count/dau";
    let server = this.model.branch("server",branch);
    return server.mget(["name"]).then(ret=>{
        for(let k in ret){
            let v = ret[k];
            let sid = v._id;
            data["server"][sid] = v.name;
        }
    }).then(()=>{
        let opensdk = this.model.branch("opensdk",branch);
        return opensdk.mget(["name"]).then(ret=>{
            for(let k in ret){
                let v = ret[k];
                let id = v._id;
                data["opensdk"][id] = v.name;
                if(!data['sdk']) data['sdk'] = id;
            }
        })
    }).then(()=>{
        return data;
    })
}

exports.daupage = function(){
    this.output = 'json';
    let sid = this.get('sid','int');
    let sdk = this.get('sdk','string');
    if(!sid || !sdk){
        return this.error('args empty')
    }
    let $page = this.get('page', 'int') || 1;
    let $size = this.get('size', 'int') || 20;

    let dauModel = this.model.count("dau",this.branch,sdk,sid);
    let query = {"sdk":sdk,"sid":sid};
    let dbsort = {"time":-1},option={};
    return dauModel.page(query,$page, $size, dbsort, option);
}


exports.act = function(){
    let branch = this.branch;
    let data = {"branch":branch,"server":{}};
    data['sid'] = this.get("sid",'int')||1;
    this.view = "count/act";
    let server = this.model.branch("server",branch);
    return server.mget(["name"]).then(ret=>{
        for(let k in ret){
            let v = ret[k];
            let sid = v._id;
            data["server"][sid] = v.name;
        }
    }).then(()=>{
        return data;
    })
}